package livraison;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Observable;

import javax.swing.JEditorPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Trajet extends Observable {


		
	    private String roadmap = "roadmap";
	    private int size = 400;
	    private String url = "http://maps.google.com/maps/api/staticmap?";
		   
	    
	   //Mettre le panel en html
	    public Trajet() {
	    	;
	    }

	   
	    public void setRoadmap(String roadMap) {
	        this.roadmap = roadMap;
	    }
	    
	    public void setSize(int size){
	    	this.size = size;
	    }

	  
	   public void setUrlMap(String adrLivreur, String adrClient, String adrRestau[]){
		    
	    	adrLivreur = adrLivreur.replace(" ", "+");
	    	adrClient = adrClient.replace(" ", "+");
	    	
	
	    	url="http://maps.google.com/maps/api/staticmap?";
	    	url += "size="+ this.size + "x" + this.size;
	    	url += "&sensor=false";
	    	url += "&path=weight:5|color:red|enc:";
	    	url += Polyline(adrLivreur, adrClient, adrRestau) ;
	        url += "&maptype=" + this.roadmap;
	        url += "&markers=color:blue|label:L|" + adrLivreur;
	        url += "&markers=color:red|label:C|" + adrClient;
	        url += restauReplace(adrRestau) ;      
	        
	        setChanged();
			notifyObservers(url);
	    }
	   
	   public String restauReplace(String adrRestau[]){
		   
		   int i=0;
		   String urlRestau="";
		   while( i != adrRestau.length ){
			   urlRestau+="&markers=color:green|label:R|";
			   urlRestau+=adrRestau[i].replace(" ", "+");
			   i++;
		   }
		   
		   return urlRestau;
		   
	   }
	   
	   
	   public String getUrlMap(){
	    return url;
	   }
	    
	    private String Polyline(String adrLivreur, String adrClient, String adrRestau[] ) {
	    	
	    	 String url = "http://maps.googleapis.com/maps/api/directions/xml?origin=";
	    	 url += adrLivreur.replace(" ", "+");
	    	 url += "&destination=";
	    	 url += adrClient.replace(" ", "+");
	    	 int i=0;
	    	 while( i!= adrRestau.length){
	    		 url += "&waypoints=optimize:true|";
	    		 url += adrRestau[i].replace(" ", "+"); 
	    		 i++;
	    	 }
	    	 url += "&sensor=false";
	    	 //System.out.println(url);
	    	 try{
	    	  URL googleMapsDistance = new URL(url);
	          URLConnection googleMapsDistanceConnection = googleMapsDistance.openConnection();		 
	          return readPolylineXML(googleMapsDistanceConnection.getInputStream());
	          }
	          catch(Exception e){
	        	  //System.out.println(e.getMessage());
	        	  return "";
	          }
	    	
	    }
	    
	    private String readPolylineXML(InputStream xml) throws Exception
	    {
	        try  
	        {
	        	//Ouvrir le xml
	            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	            Document leXml = docBuilder.parse(xml);
	            
	            //Aller chercher l'�l�ment contenant le Polyline
	            leXml.getDocumentElement().normalize();
	            Element e = (Element)leXml.getElementsByTagName("overview_polyline").item(0);
	            		         
	            CharacterData caract = (CharacterData)e.getElementsByTagName("points").item(0).getFirstChild();
	            
	            return caract.getData();
	        }
	        catch (Exception e)
	        {
	            //System.out.println(e.getMessage());
	            throw e;
	        }
	    }
}
